package clear.samples.android.integration;

import java.util.ArrayList;

import clear.services.ClearDataService;
import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;

import air.Main.AppEntry;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class CustomActivity extends AppEntry {
	public static CustomActivity instance;
	private Intent clearDataServiceIntent;

	public CustomActivity() {
		super();
		instance = this;
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// WinStoneManager wm = new WinStoneManager();
		// String localPath = "/WEB-INF/web.xml";
		// wm.start(localPath);

		super.onCreate(savedInstanceState);
		clearDataServiceIntent = new Intent(this, ClearDataService.class);
		startService(clearDataServiceIntent);

	}

	public void recognizeVoice(int requestCode, String prompt) {
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
		startActivityForResult(intent, requestCode);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 12345 && resultCode == RESULT_OK) {
			// Fill the list view with the strings the recognizer thought it
			// could have heard
			ArrayList<String> matches = data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

			MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
			AsyncMessage msg = new AsyncMessage();

			msg.setDestination("voiceRecognition");
			msg.setClientId(UUIDUtils.createUUID(true));
			msg.setMessageId(UUIDUtils.createUUID(true));
			msg.setTimestamp(System.currentTimeMillis());
			msg.setBody(matches);
			msgBroker.routeMessageToService(msg, null);
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	private static final int OPTIONS_DIALOG = 1;

	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case OPTIONS_DIALOG:
			final CharSequence[] items = { "Edit", "View Associates", "Delete",
					"Back to list" };

			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Select an action");
			builder.setItems(items, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int item) {
					Toast.makeText(getApplicationContext(), items[item],
							Toast.LENGTH_SHORT).show();
				}
			});
			AlertDialog alert = builder.create();
			dialog = alert;
			break;
		/*
		 * case DIALOG_GAMEOVER_ID:
		 * 
		 * ProgressDialog progressDialog = ProgressDialog.show(this, "",
		 * "Loading. Please wait...", true);
		 * 
		 * dialog = progressDialog; break;
		 */
		default:
			dialog = null;
		}
		return dialog;
	}
	
	public void onResume() {
		stopService(clearDataServiceIntent);
		super.onResume();
	}

}